import pandas as pd
import math
import numpy as np
import decimal

global i

# people = pd.read_excel('test.xlsx', header = 2, sheet_name ='sheet 1')
# data01 = pd.read_excel(r'C:\Users\Whaldom\Desktop\Desktop(2)\三峡响水近海2\响水近海风电场2 #12.xlsx', sheet_name ='Sheet1')
data01 = pd.read_excel(r'D:\数据分析(DA)\Python\PythonSmallStream\Projects\孪生功率曲线\三峡响水近海2\响水近海风电场2 #12.xlsx', sheet_name ='Sheet1')
print(data01)
#people.columns = ['id', 'name', 'age']
#print(people.columns)
#print(data1)
inx = data01['风速']
iny = data01['功率']
data_x = inx.values
data_y = iny.values


k = 0
i = 0
countRight = 0
countLeft = 0
counter = 0

a = 50.98
b = -237.38

x_temp = []  # 曲线上所有点集合
x_left = []  # 左侧点集合
x_right = []  # 右侧点集合
x_l = []  # 左侧点，求差
x_r = []  # 右侧点，求差

x_leftX = [] # 存调整点


# y = 50.7966*pow(x,2) - 236.53*x + 2.69
# 一元二次方程求根公式：x = (-b +_ sqrt(pow(b) - 4ac)) / 2a

# ValueError: math domain error
# 原因：操作不符合数学定义，如对负数取对数，对负数开平方。


for v in data_x:
#for v in range(1,30):
    counter += 1
    c = 270 - data_y[k]
    # print(a, b, c)
    # (x, y)是曲线上的点坐标
    x = (-b + math.sqrt(pow(b, 2)- 4 * a * c)) / (2 * a)
    y = a * pow(x, 2) - 236.53 * x + 270
    # (x01, y01)曲线上点坐标，保留2位小数
    # print(data_x[k])
    x01 = round(x, 2)
    y01 = round(y, 2)
    x_temp.append([x01, y01])
    # x_l.append(data_x[k] - x01)
    # print(x_l)
    if x < data_x[k]:  # 点在曲线右侧
        x_right.append(x_temp.pop())
        x_r.append(round(data_x[k] - x, 2))
        # print(x_right)
        countRight += 1
    else:  # 点在曲线左侧或落在曲线上
        x_left.append(x_temp.pop())
        x_leftX.append(data_x[k])
        x_l.append(round(x01 - data_x[k], 2))
        countLeft += 1
    k += 1
print('x_l:')
print(x_l)
print('x_r')
print(x_r)
print(r'程序计数器：' + str(counter))
print(r'曲线右侧点数：' + str(countRight))
print(r'曲线左侧点数：' + str(countLeft))
# print(x_left)
# print(x_right)

# 函数：y = a * x + b
# 变形：b = y_a - a * x_a

#for move in x_l:

# 修正曲线左侧点位
'''while np.nan in x_l:
    x_l.remove(np.nan)'''
new_list = []
# 去除NaN
for elem in x_l:
    if not np.isnan(elem):
        new_list.append(elem)

# 求方差
xR = np.std(x_r)
xL = np.std(new_list)

print('xR方差：' + str(xR))
print('xL方差：' + str(xL))  # nan
print('综合方差：' + str(np.std(xR + xL)))


# 平移曲线
# aLine = 0.99
# bLine = -2.00

# 
i2 = 0
pi = 0
q = 0
i_Left = []

# moveI = 0
listMoveX = []
moveCounter = 0


'''
# a=np.array([[9.96, 2968.08], [9.17, 2388.64], [7.78, 1515.02]])
x_leftNp = np.array(x_left)
for moveI in range(0,len(x_left)):
    x_leftNp[moveI][0] += aMove
'''

''' v1.0
new_list02 = []
print('当前方差系数：' + str((xR - xL)/xR))
while condition > 0.01 or condition < 0:
    for i5 in x_left:
        xMove = aMove * i5[0] + bMove
        listMoveX.append(xMove)
    for elem in listMoveX:
        if not np.isnan(elem):
            new_list02.append(elem)
    xL = np.std(new_list02)
    condition = (xR - xL)/xR
    print(xR, xL)
    aMove += -0.01
    bMove += 0.01
    moveCounter += 1
'''
# v2.0

aMove = 1
bMove = -1 # 上限：1
bMoveList = []
xMoved = 0
new_list02 = []
condition = (xR - xL)/xR

print('当前方差系数：' + str(condition))
while (condition > 0.01) or (condition < 0):
    # print('ComeIn')
    while bMove < 1:
        bMove += 0.01
        listMoveX = []
        for i5 in x_left:
            xMoved = round(aMove * i5[0] + bMove, 2)
            listMoveX.append(xMoved)
            bMoveList.append(bMove)
            moveCounter += 1
        # print(listMoveX)
        # 去除空值
        for elem in listMoveX:
            if not np.isnan(elem):
                new_list02.append(elem)
        xL = np.std(new_list02)
        condition = (xR - xL) / xR
        new_list02 = []
        if (condition < 0.01) :
            break
    bMove = -1
    print(condition)
    aMove -= 0.01

print('y = ax + b: ')
print('a = ' + str(round(aMove,2)) + '\nb = ' + str(bMoveList[-1]) + '\nb取值次数：' + str(len(bMoveList)))
    
print('移动后方差系数：' + str((xR - xL)/xR))
print('xL方差(移动' + str(moveCounter) + ')次：' + str(xL))
    # print(listMoveX)

    
'''
x_leftX = [p - 0.01 for p in x_leftX]
xMove = aMove * x_leftX + bMove
xL = np.std(x_leftX)
print('xL方差：' + str(xL))
'''

'''
x_left01 = []
for pi in x_left:
    x_left01.append(x_left[pi][1])
    pi += 1
print(x_left01)
'''
# i_Left = [p + 1 for p in x_left[p][1]]

# x02 = round(aLine * i_Left[i2], 2)
# print(x02)
#print(x_left)
#print(i_Left)
#y02 = round(x_left)

'''while (xR - xL)/xR > 0.01:
    # xR:1.07, xL:0.43，左移, a减小0.01
    i_Left = [p - 0.01 for p in new_list]
    print(i_Left)
    #for()
'''    

'''
#平移：0.633
while (xR - xL)/xR > 0.01:
    i_Left = [p + 0.633 for p in new_list]
    # print(i_Left)
    xL = np.std(i_Left)
# np.set_printoptions(threshold='nan')
stdNpListLeft = np.array(i_Left)
xLnp = np.round(stdNpListLeft, 2)
print(xLnp)
'''

'''
print('xR方差：' + str(xR))
print('xL方差：' + str(xL))  # nan
print('综合方差：' + str(np.std(xR + xL)))

'''
'''
a = 1--0.01
b = 0
y = 0.99 x + 0
'''

'''for v in data2:
    k += 1
    while a < 1:
        a += 0.01
        b = y1 - a * data2[k]
        print(data2[k])
        print(str(a) + ':' + str(b))
'''

'''
for v in data_x:
    a = 0.5
    while a < 1:
        i += 1
        b = data_y[k] - a * data_x[k]
        print(str(a)+'------'+str(b))
        a += 0.01
    k += 1
'''
